package com.gx.core
{
	import com.gx.interfaces.IComponent;
	import com.gx.interfaces.IPhysical;
	import com.gx.interfaces.IPhysicsLayer;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;

	
	public class Component extends EventDispatcher implements IComponent
	{
		
		private var _id:int;
		public function get id():int {return _id;}
		public function set id(value:int):void {_id = value;}
		
		private var _parent:IComponent;
		public function get parent():IComponent {return _parent;}
		public function set parent(value:IComponent):void {_parent = value;}
		
		public function Component()
		{
	
		}
		
		public function init():void
		{
			
		}
		
		private var _components:Array = new Array();
		public function get components():Array {return _components;}
		public function set components(value:Array):void 
		{
			for each(var c:Component in value)
			{
				addComponent(c);
			}
		}
		
		public function addComponent(c:IComponent):void
		{
			_components.push(c);
			c.parent = this;
			if(c is IPhysicsLayer)
			{
				IPhysicsLayer(c).init();
			}
			if(c is IPhysical)
			{
				IPhysical(c).init();
			}
		}
		
		override public function dispatchEvent(event:Event):Boolean
		{
			
			if(parent != null)
			{
				this.parent.dispatchEvent(event);
			}
			return super.dispatchEvent(event);
		}
		
		
	}
}